package com.hanhanxiaochong.zhongxiangchuanmei.view.my.act;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.alipay.Alipay;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.BankCardBean;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.WXPayBean;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.my.RechargePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.SharedPreferencesUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.StringUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.home.act.AppointmentSuccessActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IRechargeView;
import com.hanhanxiaochong.zhongxiangchuanmei.wxapi.WXPayUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/6/11.
 * Desc   : 充值Activity
 */

public class RechargeActivity extends BaseActivity<RechargePresenter> implements IRechargeView {

    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.et_input)
    EditText etInput;
    @BindView(R.id.img_choose_wechatpay)
    ImageView imgChooseWechatpay;
    @BindView(R.id.img_choose_alipay)
    ImageView imgChooseAlipay;
    @BindView(R.id.ll_wechatpay)
    LinearLayout llWechatpay;
    @BindView(R.id.ll_alipay)
    LinearLayout llAlipay;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bankcard_last_number)
    TextView tvBankcardLastNumber;
    @BindView(R.id.ll_bankinfo)
    LinearLayout llBankinfo;
    @BindView(R.id.txt_band_bankcard)
    TextView txtBandBankcard;
    @BindView(R.id.ll_not_bind_bankcard)
    LinearLayout llNotBindBankcard;
    @BindView(R.id.txt_recharge)
    TextView txtRecharge;

    private String token;
    private boolean rechargeFlag = true;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_recharge);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new RechargePresenter(this);
    }

    @Override
    protected void init() {
        txtTitle.setText("充值");
        token = SharedPreferencesUtil.getsInstances(this).getLoginInfo().getToken();
        mPresenter.getBankCards(token);
        setRechargeType();
    }

    private void setRechargeType() {
        if (rechargeFlag) {
            imgChooseWechatpay.setVisibility(View.VISIBLE);
            imgChooseAlipay.setVisibility(View.GONE);
        } else {
            imgChooseWechatpay.setVisibility(View.GONE);
            imgChooseAlipay.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.ll_leftview, R.id.ll_wechatpay, R.id.ll_alipay, R.id.ll_bankinfo, R.id
            .txt_band_bankcard, R.id.txt_recharge})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_leftview:
                finish();
                break;
            case R.id.ll_wechatpay:
                rechargeFlag = true;
                setRechargeType();
                break;
            case R.id.ll_alipay:
                rechargeFlag = false;
                setRechargeType();
                break;
            case R.id.ll_bankinfo:
                break;
            case R.id.txt_band_bankcard:
                Intent intent = new Intent(this, AddBankCardActivity.class);
                startActivity(intent);
                break;
            case R.id.txt_recharge:
                if (StringUtils.isEmpty(etInput.getText().toString())) {
                    mPresenter.recharge(token, etInput.getText().toString(), rechargeFlag);
                } else {
                    showTip("请输入要充值的金额");
                }
                break;
        }
    }


    @Override
    public void wxPay(WXPayBean wxPayBean) {
        doWXPay(wxPayBean);
    }

    //微信支付
    private void doWXPay(WXPayBean pay_param) {
        WXPayUtils.WXPayBuilder builder = new WXPayUtils.WXPayBuilder();
        builder.setAppId(pay_param.getAppid())
                .setPartnerId(pay_param.getPartnerid())
                .setPrepayId(pay_param.getPrepayid())
                .setPackageValue(pay_param.getPackageX())
                .setNonceStr(pay_param.getNoncestr())
                .setTimeStamp(pay_param.getTimestamp())
                .setSign(pay_param.getSign())
                .build().toWXPayNotSign(this, pay_param.getAppid());
    }

    @Override
    public void aliPay(String string) {
        new Alipay(this, string, new Alipay.AlipayResultCallBack() {
            @Override
            public void onSuccess() {
                Intent intent = new Intent(mContext, AppointmentSuccessActivity.class);
                intent.putExtra("result", "1");
                startActivity(intent);
            }

            @Override
            public void onDealing() {
                ToastUtils.showToast("支付处理中...");
            }

            @Override
            public void onError(int error_code) {
                switch (error_code) {
                    case Alipay.ERROR_RESULT:
                        ToastUtils.showToast("支付失败:支付结果解析错误");
                        break;

                    case Alipay.ERROR_NETWORK:
                        ToastUtils.showToast("支付失败:网络连接错误");
                        break;

                    case Alipay.ERROR_PAY:
                        ToastUtils.showToast("支付错误:支付码支付失败");
                        break;

                    default:
                        ToastUtils.showToast("支付错误");
                        break;
                }
            }

            @Override
            public void onCancel() {
                ToastUtils.showToast("支付取消");
            }
        }).doPay();
    }

    @Override
    public void setBankcard(List<BankCardBean> data) {
        if (data.size() > 0) {
            llBankinfo.setVisibility(View.VISIBLE);
            llNotBindBankcard.setVisibility(View.GONE);
            tvBankName.setText(data.get(0).getCa_bankname().substring(0, 4));
            tvBankcardLastNumber.setText(data.get(0).getCa_number().replaceAll(" ", "").
                    substring(data.get(0).getCa_number().replaceAll(" ", "").length() - 4,
                            data.get(0).getCa_number().replaceAll(" ", "").length()));
        }
    }
}
